<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品公共类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */

class commonGoods {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 图片缩略图
     *
     * @param  $src_file	源图片名称
     * @param  $dest_file	目标图片名称
     * @param  $new_size	图片的大小
     * @param  $quantity	图片的质量
     * @return unknown
     */
    public function ResizeImage( $src_file="" , $dest_file="" , $new_size = array('width'=>308,'height'=>288) , $quantity="100" ){
        $imginfo = getimagesize( $src_file );
        if ($imginfo == NULL){
            error_log("file name is NULL");
            return false;
        }
        // GD can only handle JPG & PNG images
        if ($imginfo[2] != JPG && $imginfo[2] != PNG && $imginfo[2] != GIF){
            error_log("file is not be supported");
            die("file is not be supported");
            return false;
        }
        $srcWidth = $imginfo[0];
        $srcHeight = $imginfo[1];
        $radio=max(($srcWidth/$new_size['width']),($srcHeight/$new_size['height']));
        $destWidth=(int)$srcWidth/$radio;
        $destHeight=(int)$srcHeight/$radio;
        if ($imginfo[2] == JPG)
            $src_img = imagecreatefromjpeg($src_file);
        elseif($imginfo[2] == PNG){
            $src_img = imagecreatefrompng($src_file);
        }
        else
            $src_img = imagecreatefromgif($src_file);

        if (!$src_img){
            error_log("file type is not found");
            return false;
        }

        $dst_img = imagecreatetruecolor($destWidth, $destHeight);
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
        if ($imginfo[2] == JPG)
            imagejpeg($dst_img, $dest_file, $quantity);
        elseif($imginfo[2] == PNG)
            imagepng($dst_img, $dest_file);
        else
            imagegif($dst_img, $dest_file);

        imagedestroy($src_img);
        imagedestroy($dst_img);

        // Set mode of uploaded picture
        chmod($dest_file, 0755);
        // We check that the image is valid
        $imginfo = @getimagesize($dest_file);
        if ($imginfo == NULL){
            @unlink($dest_file);
            return false;
        } else {
            return true;
        }
    }
    
    /**
     * 查询商品图片
     * @param array $arg_array 参数数组
     * @return null|array
     */
    public function searchGoodsPicture( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //网购号
        if (isset($arg_array['postID']) && !empty($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['postID'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_picture ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_picture ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'postID' => $item->postID,
                    'pictureLink' => G_IMAGE_URL . 'goods/' . $item->pictureLink,
                    'orgitalLink' => $item->pictureLink,
                    'sortOrder' => $item->sortOrder,
                    'mainFlg' => $item->mainFlg
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
}